﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using One2Team.EasyScrum.UI.Web.Models;

namespace One2Team.EasyScrum.UI.Web.Controllers
{
    public class UserController : Controller
    {
        private easyscrumEntities db = new easyscrumEntities();

        //
        // GET: /User/

        public ActionResult Index()
        {
            var user = db.user.Include(u => u.project).Include(u => u.role).Include(u => u.team);
            return View(user.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id = 0)
        {
            user user = db.user.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.id_project = new SelectList(db.project, "id", "nom");
            ViewBag.id_role = new SelectList(db.role, "id", "libelle");
            ViewBag.id_team = new SelectList(db.team, "id", "teamname");
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(user user)
        {
            if (ModelState.IsValid)
            {
                db.user.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_project = new SelectList(db.project, "id", "nom", user.id_project);
            ViewBag.id_role = new SelectList(db.role, "id", "libelle", user.id_role);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user.id_team);
            return View(user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id = 0)
        {
            user user = db.user.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", user.id_project);
            ViewBag.id_role = new SelectList(db.role, "id", "libelle", user.id_role);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user.id_team);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(user user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", user.id_project);
            ViewBag.id_role = new SelectList(db.role, "id", "libelle", user.id_role);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user.id_team);
            return View(user);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id = 0)
        {
            user user = db.user.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            user user = db.user.Find(id);
            db.user.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}